package com.loveyukari.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loveyukari.model.Auth;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年7月26日下午4:19:47
**/
@Service
public class DrawChatmsgService {

	private Logger log = LoggerFactory.getLogger(DrawChatmsgService.class);
	
	@Autowired
	private AuthService authService;
	
	private Date startTime;
	private String chatmsg;
	private boolean drawChatmsgFlag;
	private Date endTime;
	private Set<Integer> drawAuthSet;
	private List<Integer> drawAuthIds;
	private int count = 1;
	private int minBl;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public String start(String chatmsg,int count,int minBl){
		this.startTime = new Date();
		log.info("start draw chatmsg "+startTime);
		this.chatmsg = chatmsg;
		this.count = count;
		this.minBl = minBl;
		this.drawAuthSet = new HashSet<Integer>();
		this.drawAuthIds = new ArrayList<Integer>();
		this.drawChatmsgFlag = true;
		return sdf.format(this.startTime);
	}
	
	public JSONObject stop(){
		this.drawChatmsgFlag = false;
		this.endTime = new Date();
		JSONArray authArray = new JSONArray();
		if (!this.drawAuthSet.isEmpty()){
			Random random = new Random();
			List<Integer> list = new ArrayList<Integer>(this.drawAuthSet);
			while(true){
				if (list.size() == 0){
					break;
				}
				int index = random.nextInt(list.size());
				Integer uid = list.get(index);
				Auth auth = authService.getCacheAuth(uid);
				if (auth != null){
					this.drawAuthIds.add(auth.getId());
					JSONObject jsonAuth = new JSONObject();
					jsonAuth.put("id", auth.getId());
					jsonAuth.put("name", auth.getName());
					jsonAuth.put("lv", auth.getLv());
					jsonAuth.put("fansLv", auth.getFansLv());
					authArray.add(jsonAuth);
					list.remove(index);
				}
				if (this.drawAuthIds.size() == count){
					break;
				}
			}
		}
		log.info("end draw chatmsg "+this.endTime+", authIds="+this.drawAuthIds+",size="+this.drawAuthSet.size()+",set "+this.drawAuthSet);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("endTime", sdf.format(this.endTime));
		jsonObject.put("authArray", authArray);
		jsonObject.put("size", this.drawAuthSet.size());
		return jsonObject;
	}
	
	public void clear(){
		this.drawChatmsgFlag = false;
		this.startTime = null;
		this.endTime = null;
		this.drawAuthSet = null;
		this.drawAuthIds = null;
	}
	
	public JSONObject getStatus(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("drawChatmsgFlag", this.drawChatmsgFlag);
		jsonObject.put("count", this.count);
		jsonObject.put("minBl", this.minBl);
		if (chatmsg != null){
			jsonObject.put("chatmsg", this.chatmsg);
		}
		if (this.startTime != null){
			jsonObject.put("startTime", sdf.format(this.startTime));
		}
		if (this.endTime != null){
			jsonObject.put("endTime", sdf.format(this.endTime));
		}
		if (this.drawAuthSet != null && !this.drawChatmsgFlag){
			jsonObject.put("size", this.drawAuthSet.size());
		}
		if (this.drawAuthIds != null && !this.drawChatmsgFlag){
			JSONArray jsonArray = new JSONArray();
			for (Integer id : this.drawAuthIds) {
				Auth auth = authService.getCacheAuth(id);
				JSONObject jsonAuth = new JSONObject();
				jsonAuth.put("id", auth.getId());
				jsonAuth.put("name", auth.getName());
				jsonAuth.put("lv", auth.getLv());
				jsonAuth.put("fansLv", auth.getFansLv());
				jsonArray.add(jsonAuth);
			}
			jsonObject.put("authArray", jsonArray);
		}
		return jsonObject;
	}
	
	public List<JSONObject> getAuthDetail(){
		List<JSONObject> result = new ArrayList<JSONObject>();
		if (drawAuthSet == null){
			return result;
		}
		for (Integer id : drawAuthSet) {
			Auth auth = authService.getCacheAuth(id);
			JSONObject authJson = new JSONObject();
			authJson.put("id", auth.getId());
			authJson.put("name", auth.getName());
			authJson.put("lv", auth.getLv());
			authJson.put("fansLv", auth.getFansLv());
			result.add(authJson);
		}
		return result;
	}
	
	public boolean isDrawChatmsg(String txt){
		return this.drawChatmsgFlag && StringUtils.equals(this.chatmsg, txt);
	}
	
	public boolean isFans(String bnn,int bl){
		return (this.minBl <= 0) || (StringUtils.equals(bnn, "196") && bl >= this.minBl);
	}
	
	public void addAuth(Integer id){
		this.drawAuthSet.add(id);
	}
}
